package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.DeviceMapper;
import com.kaili.basic.mapper.MdmDeviceOnlineMapper;
import com.kaili.basic.pojo.dto.MdmDeviceOnlineAddDto;
import com.kaili.basic.pojo.dto.MdmDeviceOnlineQueryDto;
import com.kaili.basic.pojo.dto.MdmDeviceOnlineUpdateDto;
import com.kaili.basic.pojo.entity.MdmDeviceOnline;
import com.kaili.basic.pojo.entity.MdmDeviceOnlineExport;
import com.kaili.basic.pojo.vo.MdmDeviceOnlineDetailVo;
import com.kaili.basic.pojo.vo.MdmDeviceOnlineVo;
import com.kaili.basic.service.MdmDeviceOnlineService;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: shenliqiang
 * @CreateDate: 2019/7/24 11:19
 * @Version: 1.0
 */
@Service
@Transactional
public class MdmDeviceOnlineServiceImpl implements MdmDeviceOnlineService {

    @Autowired
    MdmDeviceOnlineMapper mdmDeviceOnlineMapper;

    @Autowired
    DeviceMapper deviceMapper;
    /**
    * @Desc:  创建设备时调用此方法创建设备
    * @author: shenliqiang
    * @Time: 2019/7/30 10:44
    * @param dto
    * @return int
    */

    @Override
    public RestStatus addMdmDeviceOnline(MdmDeviceOnlineAddDto dto) {
        MdmDeviceOnline mdmDeviceOnline = new MdmDeviceOnline();
        BeanUtils.copyProperties(dto, mdmDeviceOnline);
        mdmDeviceOnline.setCreateBy(LoginDetailUtils.getCurrent().getId());
        mdmDeviceOnlineMapper.addMdmDeviceOnline(mdmDeviceOnline);
        return CommonStatus.ADD_OK;
    }
    /**
    * @Desc:  删除设备时调用此接口删除设备
    * @author: shenliqiang
    * @Time: 2019/7/30 10:44
    * @param deviceIds
    * @return int
    */

    @Override
    public RestStatus deleteMdmDeviceOnlines(Long[] deviceIds) {
        Map<String,Object> map = new HashMap<>();
        map.put("modifyBy", LoginDetailUtils.getCurrent().getId());
        map.put("deviceIds",deviceIds);
        mdmDeviceOnlineMapper.deleteMdmDeviceOnlines(map);
        return CommonStatus.DELETE_OK;
    }

    /**
    * @Desc:  更新设备在线状态--设备
    * @author: shenliqiang
    * @Time: 2019/7/30 10:45
    * @param dto
    * @return int
    */

    @Override
    public RestStatus updateMdmDeviceOnline(MdmDeviceOnlineUpdateDto dto) {
        if(dto.getImei().equals("")&&dto.getSn().equals("")){
            return CommonStatus.DEVICE_SNIMEI_NOTNULL;
        }if(dto.getImei()!=null){
            dto.setFacilityId(deviceMapper.getDeviceByImei(dto.getImei()).getId());
        }else{
            dto.setFacilityId(deviceMapper.getDeviceBySn(dto.getSn()).getId());
        }
        MdmDeviceOnline mdmDeviceOnline = new MdmDeviceOnline();
        BeanUtils.copyProperties(dto, mdmDeviceOnline);
        mdmDeviceOnline.setModifyBy(LoginDetailUtils.getCurrent().getId());
        mdmDeviceOnlineMapper.updateMdmDeviceOnline(mdmDeviceOnline);
        return CommonStatus.DEVICD_UPDATE_SUCCESS;
    }

    /**
    * @Desc:  分页查询
    * @author: shenliqiang
    * @Time: 2019/7/30 10:45
    * @param page
    * @param mdmDeviceOnlineQueryDto
    * @return com.baomidou.mybatisplus.plugins.Page<com.kaili.basic.pojo.vo.MdmDeviceOnlineVo>
    */

    @Override
    public Page<MdmDeviceOnlineVo> queryMdmDeviceOnline(Page<MdmDeviceOnlineVo> page, MdmDeviceOnlineQueryDto mdmDeviceOnlineQueryDto) {
        List<MdmDeviceOnlineVo> mdmDeviceOnlines = mdmDeviceOnlineMapper.queryMdmDevice(page,mdmDeviceOnlineQueryDto);
        return page.setRecords(mdmDeviceOnlines);
    }

    /**
    * @Desc:  导出操作
    * @author: shenliqiang
    * @Time: 2019/7/30 10:46
    * @param mdmDeviceOnlineQueryDto
    * @return java.util.List<com.kaili.basic.pojo.entity.MdmDeviceOnlineExport>
    */

    @Override
    public List<MdmDeviceOnlineExport> queryMdmDeviceOnlineExport(MdmDeviceOnlineQueryDto mdmDeviceOnlineQueryDto) {
        return mdmDeviceOnlineMapper.queryMdmDeviceOnlineExport(mdmDeviceOnlineQueryDto);
    }

    /**
    * @Desc:  查看设备详细信息
    * @author: shenliqiang
    * @Time: 2019/7/30 10:46
    * @param deviceOnlineId
    * @return com.kaili.basic.pojo.vo.MdmDeviceOnlineDetailVo
    */

    @Override
    public MdmDeviceOnlineDetailVo queryOnlineDeviceDetail(Long deviceOnlineId) {
        MdmDeviceOnlineDetailVo vo = mdmDeviceOnlineMapper.queryOnlineDeviceDetail(deviceOnlineId);
        return vo;
    }
}
